﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using SharedLibraries;
using SharedLibraries.Constants;
using EntityObject;
using DataAccessObject;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.DB;
using ThangNM_LIB.EO;

namespace ThangNM.UserControl
{
    public partial class QuestionAnswer_DetailUC : System.Web.UI.UserControl
    {
        #region "Properties & Event"
        public delegate void CustomerEventHandler(object sender, CustomerEventArgs args);
        public event CustomerEventHandler ViewMessage;
        public event EventHandler BackList;

        public Int16 iFunctionType
        {
            get { return (Int16)ViewState["iFunctionType"]; }
            set { ViewState["iFunctionType"] = value; }
        }

        public QuestionAnswerEO objQuestionAnswerEO_Temp
        {
            get { return (QuestionAnswerEO)ViewState["objQuestionAnswerEO_Temp"]; }
            set { ViewState["objQuestionAnswerEO_Temp"] = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                txtsContent.EnterMode = CKEditor.NET.EnterMode.BR;
                LoadCKEditor(txtsContent);
                if (!IsPostBack)
                {
                    ToolBarUC1.ViewGroupButtonByFunction(ConstEx.funcButtonToolBar.DETAIL);
                }
            }
            catch (Exception ex)
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.ERROR, ex.Message, null));
            }
        }

        public void BindDataDetail(QuestionAnswerEO _QuestionAnswerEO, Int16 iFuncType)
        {
            objQuestionAnswerEO_Temp = _QuestionAnswerEO;
            BindData_DropDownList();
            QuestionAnswerExEO _QuestionAnswerExEO = new QuestionAnswerExEO();
            object temp = JsonEx.Json2Object(_QuestionAnswerEO.sQuestionAnswerEx, _QuestionAnswerExEO.GetType());
            if (temp != null)
                _QuestionAnswerExEO = (QuestionAnswerExEO)temp;
            //hfPK_iQuestionID.Value = Convert.ToString(_QuestionAnswerEO.PK_iQuestionID);
            //hfsTitle.Value = Convert.ToString(_QuestionAnswerEO.sTitle);
            //hfFK_iAccountID.Value = Convert.ToString(_QuestionAnswerEO.FK_iAccountID);
            txtFK_iAnswerID.Text = Convert.ToString(_QuestionAnswerEO.FK_iAnswerID);
            txtsName.Text = Convert.ToString(_QuestionAnswerEO.sName);
            txtsEmail.Text = Convert.ToString(_QuestionAnswerEO.sEmail);
            txtsWebsite.Text = Convert.ToString(_QuestionAnswerExEO.sWebsite);
            txtsTitle.Text = Convert.ToString(_QuestionAnswerEO.sTitle);
            txtsContent.Text = Convert.ToString(_QuestionAnswerEO.sContent);
            txtiVisit.Text = Convert.ToString(_QuestionAnswerEO.iVisit);
            txtiLike.Text = Convert.ToString(_QuestionAnswerEO.iLike);
            chkbNotification.Checked = Convert.ToBoolean(_QuestionAnswerEO.bNotification);
            ddliStatus.SelectedValue = Convert.ToString(_QuestionAnswerEO.iStatus);
            txttDateTimeCreate.Text = _QuestionAnswerEO.tDateTimeCreate == DateTime.MinValue ? DateTime.Now.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5) : _QuestionAnswerEO.tDateTimeCreate.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5);
            txttDateTimeModify.Text = _QuestionAnswerEO.tDateTimeModify == DateTime.MinValue ? DateTime.Now.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5) : _QuestionAnswerEO.tDateTimeModify.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5);
            iFunctionType = iFuncType;
            ViewByFunction(iFunctionType);
        }

        protected void BindData_DropDownList()
        {
            //ddliPermission.DataSource = GetListConstants.QuestionAnswer_iPermission_GLC();
            //ddliPermission.DataTextField = "Value";
            //ddliPermission.DataValueField = "Key";
            //ddliPermission.DataBind();

            ddliStatus.DataSource = GetListConstants.Topic_iStatus_GLC(false);
            ddliStatus.DataValueField = "Key";
            ddliStatus.DataTextField = "Value";
            ddliStatus.DataBind();
        }

        protected bool CheckInput()
        {
            string strNull = "";
            //if (string.IsNullOrEmpty(txtFK_iAnswerID.Text) == true)
            //{
            //    strNull += ", " + lblFK_iAnswerID.Text;
            //    txtFK_iAnswerID.Focus();
            //}
            if (string.IsNullOrEmpty(txtsName.Text) == true)
            {
                strNull += ", " + lblsName.Text;
                txtsName.Focus();
            }
            if (string.IsNullOrEmpty(txtsEmail.Text) == true)
            {
                strNull += ", " + lblsEmail.Text;
                txtsEmail.Focus();
            }
            if (string.IsNullOrEmpty(txtsWebsite.Text) == true)
            {
                strNull += ", " + lblsWebsite.Text;
                txtsWebsite.Focus();
            }
            if (string.IsNullOrEmpty(txtsTitle.Text) == true && iFunctionType != ConstEx.FunctionType.InsertNew && iFunctionType != ConstEx.FunctionType.InsertComment)
            {
                strNull += ", " + lblsTitle.Text;
                txtsTitle.Focus();
            }
            if (string.IsNullOrEmpty(txtsContent.Text) == true)
            {
                strNull += ", " + lblsContent.Text;
                txtsContent.Focus();
            }
            //if (string.IsNullOrEmpty(txtbStatus.Text) == true)
            //{
            //    strNull += ", " + lblbStatus.Text;
            //    txtbStatus.Focus();
            //}
            if (string.IsNullOrEmpty(txttDateTimeCreate.Text) == true)
            {
                strNull += ", " + lbltDateTimeCreate.Text;
                txttDateTimeCreate.Focus();
            }

            if (String.IsNullOrEmpty(strNull))
            {
                return true;
            }
            else
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.WARNING, MessagesEx.CAC_TRUONG_BAT_BUOC_PHAI_NHAP + strNull.Substring(2).Replace(":", ""), null));
                return false;
            }
        }

        protected void GetObject(ref QuestionAnswerEO _QuestionAnswerEO, ref SEOEO _SEOEO)
        {
            try
            {
                QuestionAnswerExEO _QuestionAnswerExEO = new QuestionAnswerExEO();
                _QuestionAnswerEO.PK_iQuestionID = objQuestionAnswerEO_Temp.PK_iQuestionID;
                _QuestionAnswerEO.FK_iAnswerID = Convert.ToInt32(txtFK_iAnswerID.Text);
                if (objQuestionAnswerEO_Temp.FK_iAccountID == 0)
                    _QuestionAnswerEO.FK_iAccountID = ShareDAO.UserDAO.getPK_iAccountID(Convert.ToString(Session["secret"])).Version;
                else
                    _QuestionAnswerEO.FK_iAccountID = objQuestionAnswerEO_Temp.FK_iAccountID;
                _QuestionAnswerEO.sName = Convert.ToString(txtsName.Text);
                _QuestionAnswerEO.sEmail = Convert.ToString(txtsEmail.Text);
                _QuestionAnswerEO.sTitle = Convert.ToString(txtsTitle.Text);
                _QuestionAnswerEO.sContent = Convert.ToString(txtsContent.Text);
                _QuestionAnswerEO.iVisit = Convert.ToInt32(txtiVisit.Text);
                _QuestionAnswerEO.iLike = Convert.ToInt32(txtiLike.Text);
                _QuestionAnswerEO.bNotification = Convert.ToBoolean(chkbNotification.Checked);
                if (iFunctionType == ConstEx.FunctionType.InsertComment)
                    _QuestionAnswerEO.iStatus = Topic_iStatus_C.Approved;
                else
                    _QuestionAnswerEO.iStatus = Convert.ToInt16(ddliStatus.SelectedValue);
                //_QuestionAnswerEO.tDateTimeCreate = Convert.ToDateTime(txttDateTimeCreate.Text);
                _QuestionAnswerExEO.sWebsite = OtherEx.getUrlStandard(txtsWebsite.Text);
                _QuestionAnswerExEO.Code = Commons.getSourceCodeUse(_QuestionAnswerEO.sContent);
                _QuestionAnswerEO.sQuestionAnswerEx = JsonEx.Object2Json(_QuestionAnswerExEO);


                Response.Cookies["autofill"].Expires = DateTime.Now.AddDays(10);
                Response.Cookies["autofill"].Value = Security.Login.Encrypt(JsonEx.Object2Json(_QuestionAnswerEO), System.Configuration.ConfigurationManager.AppSettings["FACEBOOK"]);
                String idTemp = StringEx.Trim(System.Configuration.ConfigurationManager.AppSettings["DEFAULT_ID"]);
                if (Convert.ToInt32(StringEx.getDefaultValue(_QuestionAnswerEO.PK_iQuestionID, 0)) != 0)
                    idTemp = _QuestionAnswerEO.PK_iQuestionID.ToString();
                if (_QuestionAnswerEO.FK_iAnswerID == 0)
                {
                    //Commons.setInfoSitemap(ref _SEOEO, TypeSiteMap.QuestionAnswer);
                    _SEOEO.sUrlRewrite = FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(_QuestionAnswerEO.sTitle) + "." + idTemp + FileNameByFunction.HTML;
                    //_SEOEO.loc_old = FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(objQuestionAnswerEO_Temp.sTitle) + "." + idTemp + FileNameByFunction.HTML;
                    //_SEOEO.lastmod = DateTime.Now;
                    _SEOEO.bStatus = true;
                }
                else
                {
                    QuestionAnswerEO _objQuestion = (new QuestionAnswerDAO()).SelectItem(_QuestionAnswerEO.FK_iAnswerID);
                    _SEOEO.sUrlRewrite = FileNameByFunction.Hoi_Dap_ + RewriteUrl.ConvertToUnSign(_objQuestion.sTitle) + "." + _objQuestion.PK_iQuestionID + FileNameByFunction.HTML;
                }

            }
            catch (Exception ex)
            {
                _QuestionAnswerEO = null;
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.ERROR, ex.Message, null));
            }
        }

        protected void ViewByFunction(Int16 iFuncType)
        {
            setEnableAllInput();
            switch (iFuncType)
            {
                case ConstEx.FunctionType.Insert:
                    //txtFK_iAnswerID.Enabled = true;
                    txtsName.Enabled = true;
                    txtsEmail.Enabled = true;
                    txtsWebsite.Enabled = true;
                    txtsContent.Enabled = true;
                    //txtbStatus.Enabled = true;
                    //txttDateTimeCreate.Enabled = true;
                    ToolBarUC1.ViewButtonByFunction(iFunctionType);
                    break;
                case ConstEx.FunctionType.Update: break;
                case ConstEx.FunctionType.Delete: break;
                case ConstEx.FunctionType.View:
                    //txtFK_iAnswerID.Enabled = true;
                    txtsName.Enabled = true;
                    txtsEmail.Enabled = true;
                    txtsWebsite.Enabled = true;
                    txtsContent.Enabled = true;
                    //txtbStatus.Enabled = true;
                    //txttDateTimeCreate.Enabled = true;
                    ToolBarUC1.ViewButtonByFunction(iFunctionType);
                    break;
                case ConstEx.FunctionType.InsertNew:
                    divClient1.Visible = false;
                    divClient3.Visible = false;
                    divClient4.Visible = false;
                    divClient5.Visible = false;
                    divClient6.Visible = false;
                    divClient8.Visible = false;
                    //txtFK_iAnswerID.Enabled = true;
                    txtsName.Enabled = true;
                    txtsEmail.Enabled = true;
                    txtsWebsite.Enabled = true;
                    txtsTitle.Enabled = true;
                    txtsContent.Enabled = true;
                    //txtbStatus.Enabled = true;
                    //txttDateTimeCreate.Enabled = true;
                    ToolBarUC1.ViewButtonByFunction(ConstEx.FunctionType.Insert);
                    break;
                case ConstEx.FunctionType.InsertComment:
                    divClient1.Visible = false;
                    divClient3.Visible = false;
                    divClient4.Visible = false;
                    divClient5.Visible = false;
                    divClient6.Visible = false;
                    divClient8.Visible = false;
                    divClient7.Visible = false;
                    //txtFK_iAnswerID.Enabled = true;
                    txtsName.Enabled = true;
                    txtsEmail.Enabled = true;
                    txtsWebsite.Enabled = true;
                    txtsTitle.Enabled = true;
                    txtsContent.Enabled = true;
                    //txtbStatus.Enabled = true;
                    //txttDateTimeCreate.Enabled = true;
                    ToolBarUC1.ViewButtonByFunction(ConstEx.FunctionType.Insert);
                    break;
            }
        }

        protected void setEnableAllInput()
        {
            //txtFK_iAnswerID.Enabled = false;
            txtsName.Enabled = false;
            txtsEmail.Enabled = false;
            txtsWebsite.Enabled = false;
            txtsContent.Enabled = false;
            //txtbStatus.Enabled = false;
            txttDateTimeCreate.Enabled = false;
            txttDateTimeModify.Enabled = false;
            txtiLike.Enabled = false;
            txtiVisit.Enabled = false;
        }

        protected void EventButtonToolBar_Click(object sender, CustomerEventArgs e)
        {
            if (e.iType != ConstEx.funcButtonToolBar.BACK_LIST && e.iType != ConstEx.funcButtonToolBar.INSERT_ITEM && (new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), e.iType) == false)
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.DANGER, MessagesEx.Khong_Co_Quyen_Truy_Cap, null));
                Commons.setAccessDenied(Page, null);
                return;
            }
            switch (e.iType)
            {
                case ConstEx.funcButtonToolBar.INSERT_LIST: break;
                case ConstEx.funcButtonToolBar.INSERT_ITEM:
                    InsertItem();
                    break;
                case ConstEx.funcButtonToolBar.UPDATE_ITEM:
                    UpdateItem();
                    break;
                case ConstEx.funcButtonToolBar.DELETE_LIST: break;
                case ConstEx.funcButtonToolBar.DELETE_ITEM:
                    DeleteItem();
                    break;
                case ConstEx.funcButtonToolBar.REFRESH_LIST: break;
                case ConstEx.funcButtonToolBar.EXPORT_EXCEL: break;
                case ConstEx.funcButtonToolBar.RESET_FORM:
                    ResetForm();
                    break;
                case ConstEx.funcButtonToolBar.BACK_LIST:
                    if (BackList != null)
                        BackList(this, null);
                    break;
            }
        }

        protected void InsertItem()
        {
            try
            {
                if (CheckInput() == true)
                {
                    QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
                    SEOEO _SEOEO = new SEOEO();
                    GetObject(ref _QuestionAnswerEO, ref _SEOEO);
                    if (_QuestionAnswerEO == null)
                        return;
                    if ((new QuestionAnswerDAO()).Insert(_QuestionAnswerEO, _SEOEO) == true)
                    {
                        setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.SUCCESS, String.Format(MessagesEx.Them_Thanh_Cong_Ma, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.FK_iAnswerID), null));
                    }
                    else
                    {
                        setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.WARNING, String.Format(MessagesEx.Them_That_Bai_Ma, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.FK_iAnswerID), null));
                    }
                }
            }
            catch (Exception ex)
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.ERROR, ex.Message, null));
            }
        }
        protected void UpdateItem()
        {
            try
            {
                if (CheckInput() == true)
                {
                    QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
                    SEOEO _SEOEO = new SEOEO();
                    GetObject(ref _QuestionAnswerEO, ref _SEOEO);
                    if (_QuestionAnswerEO == null)
                        return;
                    if ((new QuestionAnswerDAO()).Update(_QuestionAnswerEO, _SEOEO) == true)
                    {
                        setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.SUCCESS, String.Format(MessagesEx.Sua_Thanh_Cong_Ma_Ten, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.PK_iQuestionID, _QuestionAnswerEO.FK_iAnswerID), null));
                    }
                    else
                    {
                        setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.WARNING, String.Format(MessagesEx.Sua_That_Bai_Ma_Ten, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.PK_iQuestionID, _QuestionAnswerEO.FK_iAnswerID), null));
                    }
                }
            }
            catch (Exception ex)
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.ERROR, ex.Message, null));
            }
        }
        protected void DeleteItem()
        {
            try
            {
                QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
                SEOEO _SEOEO = new SEOEO();
                GetObject(ref _QuestionAnswerEO, ref _SEOEO);
                if (_QuestionAnswerEO == null)
                    return;
                //if (QuestionAnswerDAO.Delete(_QuestionAnswerEO.PK_iQuestionID.ToString(), _SEOEO.loc_old) == true)
                //{
                //    if (ViewMessage != null)
                //    {
                //        ViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.SUCCESS, String.Format(MessagesEx.Xoa_Thanh_Cong_Ma_Ten, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.PK_iQuestionID, _QuestionAnswerEO.FK_iAnswerID), null));
                //    }
                //}
                //else
                //{
                //    if (ViewMessage != null)
                //    {
                //        ViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.WARNING, String.Format(MessagesEx.Xoa_That_Bai_Ma_Ten, ConstEx.Table_Name_C.QuestionAnswer, _QuestionAnswerEO.PK_iQuestionID, _QuestionAnswerEO.FK_iAnswerID), null));
                //    }
                //}
            }
            catch (Exception ex)
            {
                setViewMessage(this, new CustomerEventArgs(ConstEx.FunctionSetMsg.ERROR, ex.Message, null));
            }
        }

        protected void ResetForm()
        {
            QuestionAnswerEO _QuestionAnswerEO = new QuestionAnswerEO();
            BindDataDetail(_QuestionAnswerEO, iFunctionType);
        }

        protected void LoadCKEditor(CKEditor.NET.CKEditorControl ControlId)
        {
            ControlId.config.toolbar = new object[] { 
              new object[] { "Save", "NewPage", "Preview", "-", "Templates" },
				new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" },
				new object[] { "Undo", "Redo", "-", "Find", "Replace", "-", "SelectAll", "RemoveFormat", "syntaxhighlight" },
			
				"/",
				new object[] { "Bold", "Italic", "Underline", "Strike" },
				new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
				new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
				new object[] { "BidiLtr", "BidiRtl" },
				new object[] { "Link", "Unlink", "Anchor" },
				new object[] { "Image"},
				"/"
            };
        }

        public void setViewMessage(object sender, CustomerEventArgs args)
        {
            if (ViewMessage != null)
                ViewMessage(sender, args);
        }
    }
}